<?php
/**
 * Class that controls user application
 */
class CApplication
{
	/**
	 * @var array application configuration
	 * default config file ("framework/default_config.php") is overloaded with
	 * custom application's config file ("APP_PATH/config.php")
	 */
	public $config;

	/**
	 * Runs application
	 */
	public function run()
	{
		$defaultConfig = include C_PATH.'default_config.php';
		$appConfig = include APP_PATH.'config.php';
		$this->config = array_merge($defaultConfig, $appConfig);

		date_default_timezone_set(C::app()->config['time']['zone']);

		C::log()->init();
		C::db()->init();

		C::includeDir(APP_PATH.'controllers');
		C::includeDir(APP_PATH.'models');

		$content = C::request()->process();
		echo C::controller()->renderLayout($content);
		C::app()->end();
	}

	/**
	 * Stops application
	 *
	 * This method must be used instead of "exit"
	 */
	public function end()
	{
		//call beforeExit

		C::db()->close();
		C::log()->close();

		exit(1);
	}
}